home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / recno / rec_put.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  6.2 KB  |  248 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)rec_put.c    8.2 (Berkeley) 9/7/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  39. #include "OurMalloc.h"
  40. #endif
  41.  
  42. #include <sys/types.h>
  43.  
  44. #include <errno.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. #include <db.h>
  50. #include "recno.h"
  51.  
  52. /*
  53.  * __REC_PUT -- Add a recno item to the tree.
  54.  *
  55.  * Parameters:
  56.  *    dbp:    pointer to access method
  57.  *    key:    key
  58.  *    data:    data
  59.  *    flag:    R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
  60.  *
  61.  * Returns:
  62.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
  63.  *    already in the tree and R_NOOVERWRITE specified.
  64.  */
  65. int
  66. __rec_put(dbp, key, data, flags)
  67.     const DB *dbp;
  68.     DBT *key;
  69.     const DBT *data;
  70.     u_int flags;
  71. {
  72.     BTREE *t;
  73.     DBT tdata;
  74.     recno_t nrec;
  75.     int status;
  76.  
  77.     t = dbp->internal;
  78.  
  79.     /* Toss any page pinned across calls. */
  80.     if (t->bt_pinned != NULL) {
  81.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  82.         t->bt_pinned = NULL;
  83.     }
  84.  
  85.     switch (flags) {
  86.     case R_CURSOR:
  87.         if (!ISSET(t, B_SEQINIT))
  88.             goto einval;
  89.         nrec = t->bt_rcursor;
  90.         break;
  91.     case R_SETCURSOR:
  92.         if ((nrec = *(recno_t *)key->data) == 0)
  93.             goto einval;
  94.         break;
  95.     case R_IAFTER:
  96.         if ((nrec = *(recno_t *)key->data) == 0) {
  97.             nrec = 1;
  98.             flags = R_IBEFORE;
  99.         }
  100.         break;
  101.     case 0:
  102.     case R_IBEFORE:
  103.         if ((nrec = *(recno_t *)key->data) == 0)
  104.             goto einval;
  105.         break;
  106.     case R_NOOVERWRITE:
  107.         if ((nrec = *(recno_t *)key->data) == 0)
  108.             goto einval;
  109.         if (nrec <= t->bt_nrecs)
  110.             return (RET_SPECIAL);
  111.         break;
  112.     default:
  113. einval:        errno = EINVAL;
  114.         return (RET_ERROR);
  115.     }
  116.  
  117.     /*
  118.      * Make sure that records up to and including the put record are
  119.      * already in the database.  If skipping records, create empty ones.
  120.      */
  121.     if (nrec > t->bt_nrecs) {
  122.         if (!ISSET(t, R_EOF | R_INMEM) &&
  123.             t->bt_irec(t, nrec) == RET_ERROR)
  124.             return (RET_ERROR);
  125.         if (nrec > t->bt_nrecs + 1) {
  126.             tdata.data = NULL;
  127.             tdata.size = 0;
  128.             while (nrec > t->bt_nrecs + 1)
  129.                 if (__rec_iput(t,
  130.                     t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
  131.                     return (RET_ERROR);
  132.         }
  133.     }
  134.  
  135.     if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS)
  136.         return (status);
  137.  
  138.     if (flags == R_SETCURSOR)
  139.         t->bt_rcursor = nrec;
  140.     
  141.     SET(t, R_MODIFIED);
  142.     return (__rec_ret(t, NULL, nrec, key, NULL));
  143. }
  144.  
  145. /*
  146.  * __REC_IPUT -- Add a recno item to the tree.
  147.  *
  148.  * Parameters:
  149.  *    t:    tree
  150.  *    nrec:    record number
  151.  *    data:    data
  152.  *
  153.  * Returns:
  154.  *    RET_ERROR, RET_SUCCESS
  155.  */
  156. int
  157. __rec_iput(t, nrec, data, flags)
  158.     BTREE *t;
  159.     recno_t nrec;
  160.     const DBT *data;
  161.     u_int flags;
  162. {
  163.     DBT tdata;
  164.     EPG *e;
  165.     PAGE *h;
  166.     indx_t index, nxtindex;
  167.     pgno_t pg;
  168.     size_t nbytes;
  169.     int dflags, status;
  170.     char *dest, db[NOVFLSIZE];
  171.  
  172.     /*
  173.      * If the data won't fit on a page, store it on indirect pages.
  174.      *
  175.      * XXX
  176.      * If the insert fails later on, these pages aren't recovered.
  177.      */
  178.     if (data->size > t->bt_ovflsize) {
  179.         if (__ovfl_put(t, data, &pg) == RET_ERROR)
  180.             return (RET_ERROR);
  181.         tdata.data = db;
  182.         tdata.size = NOVFLSIZE;
  183.         *(pgno_t *)db = pg;
  184.         *(size_t *)(db + sizeof(pgno_t)) = data->size;
  185.         dflags = P_BIGDATA;
  186.         data = &tdata;
  187.     } else
  188.         dflags = 0;
  189.  
  190.     /* __rec_search pins the returned page. */
  191.     if ((e = __rec_search(t, nrec,
  192.         nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
  193.         SINSERT : SEARCH)) == NULL)
  194.         return (RET_ERROR);
  195.  
  196.     h = e->page;
  197.     index = e->index;
  198.  
  199.     /*
  200.      * Add the specified key/data pair to the tree.  The R_IAFTER and
  201.      * R_IBEFORE flags insert the key after/before the specified key.
  202.      *
  203.      * Pages are split as required.
  204.      */
  205.     switch (flags) {
  206.     case R_IAFTER:
  207.         ++index;
  208.         break;
  209.     case R_IBEFORE:
  210.         break;
  211.     default:
  212.         if (nrec < t->bt_nrecs &&
  213.             __rec_dleaf(t, h, index) == RET_ERROR) {
  214.             mpool_put(t->bt_mp, h, 0);
  215.             return (RET_ERROR);
  216.         }
  217.         break;
  218.     }
  219.  
  220.     /*
  221.      * If not enough room, split the page.  The split code will insert
  222.      * the key and data and unpin the current page.  If inserting into
  223.      * the offset array, shift the pointers up.
  224.      */
  225.     nbytes = NRLEAFDBT(data->size);
  226.     if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
  227.         status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
  228.         if (status == RET_SUCCESS)
  229.             ++t->bt_nrecs;
  230.         return (status);
  231.     }
  232.  
  233.     if (index < (nxtindex = NEXTINDEX(h)))
  234.         memmove(h->linp + index + 1, h->linp + index,
  235.             (nxtindex - index) * sizeof(indx_t));
  236.     h->lower += sizeof(indx_t);
  237.  
  238.     h->linp[index] = h->upper -= nbytes;
  239.     dest = (char *)h + h->upper;
  240.     WR_RLEAF(dest, data, dflags);
  241.  
  242.     ++t->bt_nrecs;
  243.     SET(t, B_MODIFIED);
  244.     mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  245.  
  246.     return (RET_SUCCESS);
  247. }
  248.